package com.woniu.defecttracking.controller.bugs;


import com.woniu.defecttracking.annotation.AddNum;
import com.woniu.defecttracking.commens.Result;
import com.woniu.defecttracking.dto.FindPriorityDto;
import com.woniu.defecttracking.dto.FindPriortyDto;
import com.woniu.defecttracking.exception.DefectTrackingException;
import com.woniu.defecttracking.param.AddPriorityParam;
import com.woniu.defecttracking.param.FindPriorityParam;
import com.woniu.defecttracking.service.PriorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 何家欣
 * @since 2021-12-31
 */
@RestController
@RequestMapping("/priority")
public class PriorityController {

    @Autowired
    private PriorityService priorityService;
    @PostMapping("/findAllPriority")
    public Result findAllPriority(){
        //返回全部状态的
        FindPriorityDto priority = priorityService.findAllPriority();
        if (priority==null){
            return Result.getInstance(priority).setMessage("查询失败").setCode(500).setData(priority);

        }
        return Result.getInstance(priority).setMessage("查询成功").setCode(200).setData(priority);
    }
    @PostMapping("/findPriority")
    public Result findOpenPriority(){
        //返回开启状态的
        FindPriorityDto priority = priorityService.findPriority();
        if (priority==null){
            return Result.getInstance(priority).setMessage("查询失败").setCode(500).setData(priority);

        }
        return Result.getInstance(priority).setMessage("查询成功").setCode(200).setData(priority);
    }

    @PostMapping("/addPriority")
    public Result addPriority(@RequestBody AddPriorityParam addPriorityParam){

        boolean b = priorityService.addPriority(addPriorityParam);
        if (b==false){
            return Result.getInstance(b).setMessage("添加失败").setCode(500).setData(b);

        }
        return Result.getInstance(b).setMessage("添加成功").setCode(200).setData(b);
    }

    @PostMapping("/updatePriority")
    public Result updatePriority(@RequestBody AddPriorityParam updatePriority){
        boolean b = priorityService.updatePriority(updatePriority);
        if (b==false){
            return Result.getInstance(false).setMessage("修改失败").setCode(500);

        }
        return Result.getInstance(true).setMessage("修改成功").setCode(200);
    }
    @PostMapping("/deletePriority")
    public Result deleteProject(@RequestBody List<Integer> ids) throws DefectTrackingException {
        System.out.println(ids);
        boolean b = priorityService.deletePriority(ids);
        if (b==false){
            return Result.getInstance(false).setMessage("删除失败").setCode(500);

        }
        return Result.getInstance(true).setMessage("删除成功").setCode(200);
    }

    //开启
    @GetMapping("/updateStatusUp/{id}")
    public Result updateStateUp(@PathVariable Integer id){
        boolean b = priorityService.updateStatusUp(id);
        if (b==false){
            return Result.getInstance(false).setMessage("开启失败").setCode(500);

        }
        return Result.getInstance(true).setMessage("开启成功").setCode(200);
    }
    //关闭
    @GetMapping("/updateStatusDown/{id}")
    public Result updateStateDown(@PathVariable Integer id){
        boolean b = priorityService.updateStatusDown(id);
        if (b==false){
            return Result.getInstance(false).setMessage("关闭失败").setCode(500);

        }
        return Result.getInstance(true).setMessage("关闭成功").setCode(200);
    }
}

